//
//  BlindApplyVC.m
//  
//
//  Created by MTiMac02 on 2017/8/23.
//
//

#import "BlindApplyVC.h"
#import "blindApplyCell.h"
#import "BlindDetalViewController.h"
#import "IGBindListMsgApi.h"
#import "IGCommonApi.h"
#import "IGAgreeOrNotBunding.h"
#import "IGEmptyView.h"

@interface BlindApplyVC ()<UITableViewDelegate,UITableViewDataSource, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>{
    NSString *content;
}

@property (weak, nonatomic) IBOutlet UITableView *Blindtable;
@property(nonatomic,strong)IGBindListMsgApi *bindApi;
@property(nonatomic,strong)NSMutableArray *bindArray;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, copy) NSString *lastTimeDate;
@property (nonatomic, strong) IGCommonApi *commonApi;
@property (nonatomic, strong) IGAgreeOrNotBunding *agreeOrNotApi;
@end

@implementation BlindApplyVC
#pragma mark -懒加载
- (NSMutableArray *)bindArray{
    if (_bindArray) {
        _bindArray=[NSMutableArray array];
    }
    return _bindArray;
}
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title=@"绑定申请";
    self.title=NSLocalizedString(@"Link Request", nil);
    self.Blindtable.backgroundColor=[UIColor mainBackgroundColor];
    self.Blindtable.rowHeight = UITableViewAutomaticDimension;
    self.Blindtable.estimatedRowHeight = 100;
    self.Blindtable.emptyDataSetSource = self;
    self.Blindtable.emptyDataSetDelegate = self;
    [self.Blindtable registerNib:[UINib nibWithNibName:@"blindApplyCell" bundle:nil] forCellReuseIdentifier:@"blindApplyCell"];
    _pageNumber = 1;
    _lastTimeDate = @"";
    self.bindApi=[IGBindListMsgApi new];
    self.commonApi=[[IGCommonApi alloc] initWithApiType:IGiGalleryApiTypeDelBundingMsg];
    self.agreeOrNotApi = [IGAgreeOrNotBunding new];
    self.agreeOrNotApi.apiType = IGBundingResultTypeAgree;
    
    [self.Blindtable ig_setHeaderWithRefreshingBlock:^{
        _pageNumber = 1;
        _lastTimeDate = @"";
        [self requestData];
    }];
    [self.Blindtable reloadData];
}

-(void)requestData{
    _bindApi.pageNum=_pageNumber;
    _bindApi.lastTimeDate=_lastTimeDate;
    @KWeakObj(self)
    [self.bindApi startWithDecodeClass:[IGBindListMsgResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.Blindtable ig_endHeaderRefreshing];
        [selfWeak.Blindtable ig_endFooterRefreshing];
        if (responseObject) {
            IGBindListMsgResp *resp=responseObject;
            IGBindListMsgModel *model=resp.data;
            if (model.msgs&&model.msgs.count>0) {
                [selfWeak setupFooter];
            }
            else
            {
                [_Blindtable ig_endFooterRefreshingWithNoMoreData];
            }
             _lastTimeDate = model.lastTimeDate;
            if (_pageNumber == 1)
            {
                _bindArray = [NSMutableArray arrayWithArray:model.msgs];
            }
            else
            {
                [_bindArray addObjectsFromArray:model.msgs];
            }
            [self.Blindtable reloadData];
        }
        else
        {
            //空白页处理逻辑写在这里
        }
    }];

}
- (void)setupFooter
{
    @KWeakObj(self)
    [self.Blindtable ig_setFooterWithRefreshingBlock:^{
        _pageNumber++;
        [selfWeak requestData];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.Blindtable ig_beginHeaderRefreshing];
}
#pragma mark -Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _bindArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    blindApplyCell *cell =[tableView dequeueReusableCellWithIdentifier:@"blindApplyCell"];
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
    BindlistMsgModel *model=_bindArray[indexPath.row];
    cell.messageLabel.text=model.content;
    cell.status=model.status;
    if ([model.status integerValue]==3) {
        cell.selectionStyle=UITableViewCellSelectionStyleGray;
    }else{
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    __weak typeof(self) weakSelf = self;
    __weak typeof(cell) weakCell = cell;
    cell.acceptBind = ^{
        weakCell.acceptBtn.enabled = NO;
        weakSelf.agreeOrNotApi.macId = model.eId;
        weakSelf.agreeOrNotApi.btype = model.type;
        weakSelf.agreeOrNotApi.applyId = model.senderId;
        [weakSelf.agreeOrNotApi startWithDecodeClass:[IGAgreeOrNotBundingResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            weakCell.acceptBtn.enabled = YES;
            if (responseObject) {
                [weakSelf.Blindtable ig_beginHeaderRefreshing];
            }
        }];
    };
    
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BindlistMsgModel *blindModel=_bindArray[indexPath.row];
    if ([blindModel.status isEqualToString:@"3"]) {
        BlindDetalViewController *vc = [[BlindDetalViewController alloc] init];
        vc.blindModel = blindModel;
        [UIViewController ig_pushViewController:vc animated:YES];
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  55;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.f;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    BindlistMsgModel *bindMessageModel = _bindArray[indexPath.row];
    if ([bindMessageModel.status integerValue] == 3) {
        [UIAlertView bk_showAlertViewWithTitle:@"" message:NSLocalizedString(@"Deny and Delete This Application", nil) cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:@[NSLocalizedString(@"Confirm", nil)] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1) {
                [self delBindRequest:bindMessageModel indexPath:indexPath];
            }
        }];
    } else {
        [self delBindRequest:bindMessageModel indexPath:indexPath];
    }
}
#pragma mark -删除某一行
- (void)delBindRequest:(BindlistMsgModel *)model indexPath:(NSIndexPath *)indexPath {
    self.commonApi.bid = model.bid;
    [self.commonApi startWithDecodeClass:[IGCommonResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            // 删除模型
            [_bindArray removeObjectAtIndex:indexPath.row];
            // 刷新
            [self.Blindtable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }];
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return @"删除";
    return NSLocalizedString(@"Delete", nil);
}

#pragma mark -Empty
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    IGEmptyView *emptyView = [[IGEmptyView alloc] init];
//    emptyView.title = @"您还没有任何绑定申请信息~";
    emptyView.title = NSLocalizedString(@"No Binding Application Information", nil);
    emptyView.requestApi = _bindApi;
    return emptyView;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
